package mainpack;

import controlpack.TokoService;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;
import modelpack.Buku;
import modelpack.Koneksi;
import modelpack.Penjualan;
import modelpack.PenjualanDetail;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;

public class Main {

    public static void panggilcsv(TokoService toko, String alamat) throws FileNotFoundException, IOException {
        
    	try {
    		
			toko.getCon().setAutoCommit(false);
			File file = new File(alamat);
	        LineIterator it = FileUtils.lineIterator(file);
	        String line = "";
	        int i=1;
	        while(it.hasNext()){
	            line = it.nextLine().toString();
	            if(!line.contains("kode")){
	                String[] arr = line.split(",");
	                Buku b = new Buku();
	                b.setId(i);
	                b.setJudul(arr[0]);
	                b.setKode(arr[1]);
	                b.setPengarang(arr[2]);
	                b.setPenerbit(arr[3]);
	                b.setHarga(new BigDecimal(Integer.parseInt(arr[4])));
	                toko.simpan(b);
	            }
	            i++;
	        }
	        LineIterator.closeQuietly(it);
	        toko.getCon().commit();
		
    	} catch (Exception e) {
    		
			try {
				System.out.println("roll back, eror.");
				toko.getCon().rollback();
			}catch (Exception e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
			
		} finally {
			
			try {
				toko.getCon().setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
			
    }

    public static void main(String[] args) throws SQLException, ClassNotFoundException, FileNotFoundException, IOException {
    	Koneksi kon = new Koneksi();
        kon.inisialiasi();
        TokoService toko = new TokoService(kon.getConnection());
        
        /*
        // NGINPUT BUKU BARU.
        String a = "9000";
		Buku b = new Buku();
		b.setJudul("javac01");
		b.setKode("j-001");
        b.setPengarang("gw");
        b.setPenerbit("bukan");
		b.setHarga(new BigDecimal(Integer.parseInt(a)));
        //toko.simpan(b);
		System.out.println("id: "+b.getId()+"\nkode: "+b.getKode()
                +"\njudul: "+b.getJudul()+"\npengarang: "+b.getPengarang()
                +"\npenerbit: "+b.getPenerbit()+"\nharga: "+b.getHarga());
        
        // SEARCH BUKU.
        Buku c = new Buku();
		c = toko.cariId(2);
		c = toko.cariKode("j-");
        System.out.println("id: "+c.getId()+"\nkode: "+c.getKode()
                +"\njudul: "+c.getJudul()+"\npengarang: "+c.getPengarang()
                +"\npenerbit: "+c.getPenerbit()+"\nharga: "+c.getHarga());
		
		// HAPUS BUKU - BELOM.
        toko.hapus(toko.cariId(2));
        
        // NGAMBIL DATA DARI FILE CSV, DIINPUT KE DB.
        panggilcsv(toko, "/home/training/workspace/minggu3.hari3/tesPR.csv");
		
		// NGINPUT BUKU (YANG UDAH ADA DI DB) KE PENJUALAN DAN PENJUALAN DETAIL.
        Buku b = toko.cariId(53);
        Penjualan p = new Penjualan();
         p.setKasir("endy");
        p.setPembayaran(new BigDecimal(1500000));
        p.setWaktu(new Date());
        
        PenjualanDetail pd = new PenjualanDetail();
        pd.setBuku(b);
        pd.setQuantity(10);
        pd.setPenjualan(p);
        
        p.getDaftarPenjualanDetail().add(pd);
        toko.simpan2(p);
        */
        
        kon.disconnect();
        System.out.println("selesai.");
    }

}
